<?php

namespace Tbf\Bundle\UpyunBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Definition;
/**
 * This is the class that loads and manages your bundle configuration
 */
class TbfUpyunExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);
        if (empty($config['domain'])){
            throw new \Exception('you need config domain to use tbf_upyun');
        }
        $container->setParameter('tbf_upyun.domain',$config['domain']);

        $container->setDefinition('tbf_upyun.origin', new Definition())
            ->setClass('UpYun')
            ->addArgument($config['bucketname'])
            ->addArgument($config['username'])
            ->addArgument($config['password'])
            ->addArgument($config['endpoint']);

        $container->setDefinition('tbf_upyun', new Definition())
            ->setClass('Tbf\Bundle\UpyunBundle\Model\UpYun')
            ->addArgument($config['bucketname'])
            ->addArgument($config['username'])
            ->addArgument($config['password'])
            ->addArgument($config['endpoint']);

        $container->setDefinition('tbf_upyun.save', new Definition())
            ->setClass('Tbf\Bundle\UpyunBundle\Service\UpyunSave')
            ->addArgument(new Reference('tbf_upyun'))
            ->addArgument($container->getParameter('tbf_upyun.domain'));
    }
}
